<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="首先科普一下，UserAgent中文名为用户代理，简称 UA，它是一个*特殊字符串头，使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。   Okhttp走的并不是原生的http请求，因此它在header里面并没有真正的User-Agent，而是“okhttp&#x2F;版本号”这样的字符串，因为后台需要统计信息，要求传入自定义的User-A">
<meta property="og:type" content="article">
<meta property="og:title" content="OKhttp设置UserAgent的那些事儿">
<meta property="og:url" content="http://vchao.top/2021/08/25/OKhttp%E8%AE%BE%E7%BD%AEUserAgent%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B%E5%84%BF/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="首先科普一下，UserAgent中文名为用户代理，简称 UA，它是一个*特殊字符串头，使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。   Okhttp走的并不是原生的http请求，因此它在header里面并没有真正的User-Agent，而是“okhttp&#x2F;版本号”这样的字符串，因为后台需要统计信息，要求传入自定义的User-A">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-08-25T14:42:51.133Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.795Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="Android Studio">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://vchao.top/2021/08/25/OKhttp%E8%AE%BE%E7%BD%AEUserAgent%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B%E5%84%BF/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>OKhttp设置UserAgent的那些事儿 | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2021/08/25/OKhttp%E8%AE%BE%E7%BD%AEUserAgent%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B%E5%84%BF/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          OKhttp设置UserAgent的那些事儿
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-08-25 22:42:51 / 修改时间：22:37:20" itemprop="dateCreated datePublished" datetime="2021-08-25T22:42:51+08:00">2021-08-25</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>首先科普一下，UserAgent中文名为用户代理，简称 UA，它是一个*特殊字符串头，使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。</p>
</blockquote>
<blockquote>
<p>Okhttp走的并不是原生的http请求，因此它在header里面并没有真正的User-Agent，而是“okhttp/版本号”这样的字符串，因为后台需要统计信息，要求传入自定义的User-Agent。</p>
</blockquote>
<p>首先先看一下怎样正确获取User-Agent。</p>
<p>0x00-正确获取User-Agent</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">private static String getUserAgent() &#123;</span><br><span class="line">        String userAgent = &quot;&quot;;</span><br><span class="line">        if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN_MR1) &#123;</span><br><span class="line">            try &#123;</span><br><span class="line">                userAgent = WebSettings.getDefaultUserAgent(context);</span><br><span class="line">            &#125; catch (Exception e) &#123;</span><br><span class="line">                userAgent = System.getProperty(&quot;http.agent&quot;);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            userAgent = System.getProperty(&quot;http.agent&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">        StringBuffer sb = new StringBuffer();</span><br><span class="line">        for (int i = 0, length = userAgent.length(); i &lt; length; i++) &#123;</span><br><span class="line">            char c = userAgent.charAt(i);</span><br><span class="line">            if (c &lt;= &#x27;\u001f&#x27; || c &gt;= &#x27;\u007f&#x27;) &#123;</span><br><span class="line">                sb.append(String.format(&quot;\\u%04x&quot;, (int) c));</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                sb.append(c);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        return sb.toString();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>代码说明：<br>1、在Api17之后可以通过WebSettings.getDefaultUserAgent(context)获取，但是经过测试极个别手机会出现找不到类的情况，因此try-catch一下，那么第二种方式是System.getProperty(“http.agent”),这两种方式有什么不同呢？从结果上来看是第一种得到的信息更全一点；<br>第一种：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Mozilla/5.0 (Linux; Android 6.0.1; MI 4LTE Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) </span><br><span class="line">Version/4.0 Chrome/51.0.2704.81 Mobile Safari/537.36</span><br></pre></td></tr></table></figure>

<p>第二种：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Dalvik/2.1.0 (Linux; U; Android 6.0.1; MI 4LTE MIUI/6.10.13)</span><br></pre></td></tr></table></figure>
<p>2、在一些国产手机上面这个User-Agent里面会包含中文，就会报错</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">java.lang.IllegalArgumentException: Unexpected char 0x7231 at 33 in User-Agent value: Mozilla/5.0 (Linux; Android 5.1; 爱国者-X86 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36</span><br><span class="line">at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2367)</span><br><span class="line">at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)</span><br><span class="line">at android.app.ActivityThread.access$800(ActivityThread.java:151)</span><br><span class="line">at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)</span><br><span class="line">at android.os.Handler.dispatchMessage(Handler.java:110)</span><br><span class="line">at android.os.Looper.loop(Looper.java:193)</span><br><span class="line">at android.app.ActivityThread.main(ActivityThread.java:5323)</span><br><span class="line">at java.lang.reflect.Method.invokeNative(Native Method)</span><br><span class="line">at java.lang.reflect.Method.invoke(Method.java:515)</span><br><span class="line">at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)</span><br><span class="line">at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)</span><br><span class="line">at dalvik.system.NativeStart.main(Native Method)</span><br><span class="line">Caused by: java.lang.IllegalArgumentException: Unexpected char 0x7231 at 33 in User-Agent value: Mozilla/5.0 </span><br><span class="line">(Linux; Android 5.1; 爱国者-X86 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36</span><br><span class="line">at okhttp3.Headers$Builder.checkNameAndValue(Headers.java:320)</span><br><span class="line">at okhttp3.Headers$Builder.add(Headers.java:270)</span><br><span class="line">at okhttp3.Request$Builder.addHeader(Request.java:175)</span><br></pre></td></tr></table></figure>

<p>什么原因引起的呢？okhttp3.Headers$Builder.checkNameAndValue进到这个方法里面可以看到okhttp对中文进行了过滤，如果不符合条件就抛出异常IllegalArgumentException</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">private void checkNameAndValue(String name, String value) &#123;</span><br><span class="line">      if (name == null) throw new NullPointerException(&quot;name == null&quot;);</span><br><span class="line">      if (name.isEmpty()) throw new IllegalArgumentException(&quot;name is empty&quot;);</span><br><span class="line">      for (int i = 0, length = name.length(); i &lt; length; i++) &#123;</span><br><span class="line">        char c = name.charAt(i);</span><br><span class="line">        if (c &lt;= &#x27;\u001f&#x27; || c &gt;= &#x27;\u007f&#x27;) &#123;</span><br><span class="line">          throw new IllegalArgumentException(Util.format(</span><br><span class="line">              &quot;Unexpected char %#04x at %d in header name: %s&quot;, (int) c, i, name));</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      if (value == null) throw new NullPointerException(&quot;value == null&quot;);</span><br><span class="line">      for (int i = 0, length = value.length(); i &lt; length; i++) &#123;</span><br><span class="line">        char c = value.charAt(i);</span><br><span class="line">        if (c &lt;= &#x27;\u001f&#x27; || c &gt;= &#x27;\u007f&#x27;) &#123;</span><br><span class="line">          throw new IllegalArgumentException(Util.format(</span><br><span class="line">              &quot;Unexpected char %#04x at %d in %s value: %s&quot;, (int) c, i, name, value));</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>因此上段代码才会对返回结果进行过滤，如果不符合条件，会进行转码。</p>
<p>0x01-给Okhttp设置User-Agent</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Request request = new Request.Builder().url(url).removeHeader(&quot;User-Agent&quot;).addHeader(&quot;User-Agent&quot;,</span><br><span class="line">                getUserAgent()).build();</span><br><span class="line">httpClient.newCall(request).enqueue(handler);</span><br></pre></td></tr></table></figure>
<p>然后看下需求，后台要求传入<code>设备类型/APP版本号/设备型号/系统版本</code> 这样的字段。那么就可以开始了。<br>先分享一个工具类。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 系统工具类</span><br><span class="line"> */</span><br><span class="line">public class SystemUtil &#123;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 获取当前手机系统语言。</span><br><span class="line">     *</span><br><span class="line">     * @return 返回当前系统语言。例如：当前设置的是“中文-中国”，则返回“zh-CN”</span><br><span class="line">     */</span><br><span class="line">    public static String getSystemLanguage() &#123;</span><br><span class="line">        return Locale.getDefault().getLanguage();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 获取当前系统上的语言列表(Locale列表)</span><br><span class="line">     *</span><br><span class="line">     * @return  语言列表</span><br><span class="line">     */</span><br><span class="line">    public static Locale[] getSystemLanguageList() &#123;</span><br><span class="line">        return Locale.getAvailableLocales();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 获取当前手机系统版本号</span><br><span class="line">     *</span><br><span class="line">     * @return  系统版本号</span><br><span class="line">     */</span><br><span class="line">    public static String getSystemVersion() &#123;</span><br><span class="line">        return android.os.Build.VERSION.RELEASE;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 获取手机型号</span><br><span class="line">     *</span><br><span class="line">     * @return  手机型号</span><br><span class="line">     */</span><br><span class="line">    public static String getSystemModel() &#123;</span><br><span class="line">        return android.os.Build.MODEL;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 获取手机厂商</span><br><span class="line">     *</span><br><span class="line">     * @return  手机厂商</span><br><span class="line">     */</span><br><span class="line">    public static String getDeviceBrand() &#123;</span><br><span class="line">        return android.os.Build.BRAND;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 获取APP版本</span><br><span class="line">     * @param context</span><br><span class="line">     * @return</span><br><span class="line">     */</span><br><span class="line">    public static String getVersionName(Context context) &#123;</span><br><span class="line">        PackageManager packageManager = context.getPackageManager();</span><br><span class="line">        PackageInfo packageInfo;</span><br><span class="line">        String versionName = &quot;&quot;;</span><br><span class="line">        try &#123;</span><br><span class="line">            packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);</span><br><span class="line">            versionName = packageInfo.versionName;</span><br><span class="line">        &#125; catch (PackageManager.NameNotFoundException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        return versionName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后，有了工具类就可以设置UA了，</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">    private static String getUserAgent(Context context) &#123;</span><br><span class="line">        String userAgent = &quot;&quot;;</span><br><span class="line">//        APP版本</span><br><span class="line">        String versionName = SystemUtil.getVersionName(context);</span><br><span class="line">//        手机型号</span><br><span class="line">        String systemModel = SystemUtil.getSystemModel();</span><br><span class="line">//        系统版本</span><br><span class="line">        String systemVersion = SystemUtil.getSystemVersion();</span><br><span class="line">        String deviceBrand = SystemUtil.getDeviceBrand();</span><br><span class="line">        userAgent = &quot;Android/&quot; + versionName + &quot;/&quot; + deviceBrand + &quot;&quot; + systemModel + &quot;/&quot; + systemVersion;</span><br><span class="line">        return userAgent;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>然后给Okhttp设置给Okhttp设置User-Agent</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Request request = new Request.Builder().url(url).removeHeader(&quot;User-Agent&quot;).addHeader(&quot;User-Agent&quot;,</span><br><span class="line">                getUserAgent()).build();</span><br><span class="line">httpClient.newCall(request).enqueue(handler);</span><br></pre></td></tr></table></figure>

<p>至此，已经完成。</p>
<p>部分内容来自简书。<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/ddbe8c637fc5">Okhttp设置User-Agent你可能没遇到的坑</a></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
              <a href="/tags/Android-Studio/" rel="tag"># Android Studio</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/25/APP%E5%86%85%E5%AD%98%E4%BC%98%E5%8C%96%EF%BC%9A%E4%BD%BF%E7%94%A8LeakCanary%E5%87%8F%E5%B0%91%E5%86%85%E5%AD%98%E6%B3%84%E9%9C%B2/" rel="prev" title="APP内存优化：使用LeakCanary减少内存泄露">
      <i class="fa fa-chevron-left"></i> APP内存优化：使用LeakCanary减少内存泄露
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/25/SVG%20-%20%E5%9C%A8Android%E4%B8%AD%E4%BD%BF%E7%94%A8%E7%9F%A2%E9%87%8F%E5%9B%BE%E5%85%A8%E6%94%BB%E7%95%A5/" rel="next" title="SVG - 在Android中使用矢量图全攻略">
      SVG - 在Android中使用矢量图全攻略 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
